// pages/staff/report.js
const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require('../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        errcode:-1,
        keyword:'',
        paymethods:[
            {
                name:'全部',
                type:'',
            },
            {
                name: '未结清',
                type: '1',
            },
            {
                name: '已结清',
                type: '2',
            },
            {
                name: '退款中',
                type: '3',
            },
            {
                name: '换课中',
                type: '4',
            },
            {
                name: '已退费',
                type: '5',
            }
        ],
        depositarr:[
            {
                name: '全部',
                type: '',
            },
            {
                name: '未交款',
                type: '1',
            },
            {
                name: '已交款',
                type: '2',
            },
            {
                name: '已退还',
                type: '3',
            },
        ],
        reportarr:[
            {
                name: '全部',
                type: '',
            },
            {
                name: '未报到',
                type: '1',
            },
            {
                name: '已报到',
                type: '2',
            },
            {
                name: '不来',
                type: '3',
            },
            {
                name: '已退费',
                type: '4',
            },
        ],
        materialarr:[
            {
                name: '全部',
                type: '',
            },
            {
                name: '未领取',
                type: '1',
            },
            {
                name: '已领取',
                type: '2',
            },
            {
                name: '已退还',
                type: '3',
            },
        ],
        obj: {
            consult_type:1, //1: 课程顾问 2: 学管师
            consultant_id: app.globalData.admin_id,
            class_id: '',
            from: 1,//	是	string	1-执业医师 2-主治 3-技能提升
            course_name: '',//	否	string	课程名称
            pid: '',//	否	string	分类
            course_status: '',//	否	string	状态 1-为结清 2-已结清 3-退款中
            material: '',//	否	string	物料 1 未领取 2 已领取 3已退还
            deposit: '',//否	string	押金 1 未交款 2已交款 3已退还
            is_report: '',//	否	string	状态:1-未报到2-已报到3-不来4-已退费
            users: '',//	否	string	姓名/电话
            group: '',//	否	string	分组
            page: 1,//	否	string	page
        },
        qc_index:0,
        qc_list:[],
        classarr:[
            {
                name:'执业医师',
                type:1,
            },
            {
                name: '主治医师',
                type: 2,
            },
            {
                name: '技能提升',
                type: 3,
            }
        ],
        class_index:0,
        stulist:[],
        issearch:false,
        reportname: '',
        payname: '',
        depositname: '',
        materialname: '',
        zyarr:[
            {
                "id": '',//分类ID
                "title": "全部"//
            },
            {
                "id": 181,//分类ID
                "title": "技能"//
            },
            {
                "id": 182,//分类ID
                "title": "理论"//
            }
        ],
        cate_arr:[],
        cate_index:0,
        jncate:[],
        zb_arr:[
            {
                'title':'全部',
                'id':'',
            }
        ],
        zb_index:0,
        zb_item:'',
        zb_false:false,
        student_id:'',
    },
    
    classChange(e){
        var that = this;
        var index = e.detail.value;
        that.setData({
            class_index: index,
            'obj.from': that.data.classarr[index].type
        })
        if(Number(index)==0){
            that.setData({
                cate_arr: that.data.zyarr,
                cate_index:0,
                pid:'',
            })
        } else if(Number(index) == 2){
            that.setData({
                cate_arr: that.data.jncate,
                cate_index: 0,
                pid:''
            })
        }else{
            that.setData({
                cate_index: 0,
                pid: ''
            })
        }
        that.roomlist();
    },
    qcChange(e){
        var that = this;
        var index = e.detail.value;
        that.setData({
            qc_index: index,
            'obj.class_id': that.data.qc_list[index].id
        })
    },
    cateChange(e) {
        var that = this;
        var index = e.detail.value;
        that.setData({
            cate_index: index,
            'obj.pid': that.data.cate_arr[index].id
        })
    },
    seacrchclick(e){
        var that = this;
        var ph = e.detail.value.phone;
        that.setData({
            'obj.users': ph,
            'obj.page':1,
        })
        that.lists();
    },

    yejinon(){
        var that = this;
        that.setData({
            issearch: true,
            'obj.from': that.data.classarr[that.data.class_index].type,
        })
    },
    repotedivyes(){
        var that = this;
        that.setData({
            'obj.page':1,
        })
        that.lists();
    },
    sureclick(){
        var that = this;
        that.setData({
            'obj.page': 1,
            issearch: false,
        })
        that.lists();
    },
    repetclick(){
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        if (Number(app.globalData.identity) == 3) {
            var consult_type = 2;
        } else {
            var consult_type = 1;
        }
        that.setData({
            issearch:false,
            obj: {
                consult_type: consult_type, //1: 课程顾问 2: 学管师
                consultant_id: app.globalData.admin_id,
                class_id: '',
                from: 1,//	是	string	1-执业医师 2-主治 3-技能提升
                course_name: '',//	否	string	课程名称
                pid: '',//	否	string	分类
                course_status: '',//	否	string	状态 1-为结清 2-已结清 3-退款中
                material: '',//	否	string	物料 1 未领取 2 已领取 3已退还
                deposit: '',//否	string	押金 1 未交款 2已交款 3已退还
                is_report: '',//	否	string	状态:1-未报到2-已报到3-不来4-已退费
                users: '',//	否	string	姓名/电话
                group: '',//	否	string	分组
                page: 1,//	否	string	page
            },
            reportname:'',
            payname:'',
            depositname:'',
            materialname:'',
        })
        that.lists();
    },
    // 点击报道状态
    reportclick(e){
        var that = this;
        var item = e.target.dataset.item;
        // console.log(item);
        that.setData({
            'obj.is_report':item.type,
            reportname: item.name,
        })
    },
    // 点击付款状态
    paymeclick(e) {
        var that = this;
        var item = e.target.dataset.item;
        // console.log(item);
        that.setData({
            'obj.course_status': item.type,
            payname: item.name,
        })
    },
    // 点击押金
    depositclick(e) {
        var that = this;
        var item = e.target.dataset.item;
        // console.log(item);
        that.setData({
            'obj.deposit': item.type,
            depositname: item.name,
        })
    },
    // 点击物料
    materialclick(e) {
        var that = this;
        var item = e.target.dataset.item;
        // console.log(item);
        that.setData({
            'obj.material': item.type,
            materialname: item.name,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        if (Number(app.globalData.identity)==3){
            that.setData({
                'obj.consult_type': 2,
                'obj.consultant_id': app.globalData.admin_id,
            })
        }else{
            that.setData({
                'obj.consult_type': 1,
                'obj.consultant_id': app.globalData.admin_id,
            })
        }
        that.setData({
            cate_arr:that.data.zyarr,
        })
        that.lists();
        that.roomlist();
        that.jntsCate();
    },

    lists(){
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        share.wxRequest('crmapi/user/consStuList', that.data.obj, function (res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                if(Number(that.data.obj.page)==1){
                    var list = data.lists;
                }else{
                    var list = that.data.stulist.concat(data.lists);
                }
                that.setData({
                    stulist: list
                })
            }
            wx.hideLoading();
            that.setData({
                errcode:0
            })
        })
    },
    roomlist(){
        var that = this;
        var obj = {
            type:that.data.obj.from,//	是	string	1 - 执业医师 2-主治 3-技能提升
            is_type:'',//	否	string	1 - 技能 2-理论3 - 全程（type等1时才有）
            cate_id:'',//	否	string	分类ID（type等于3时）
        }
        share.wxRequest('crmapi/User/roomlist', obj, function (res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var objcata = [{
                    "id": '',//分类ID
                    "title": "全部"//
                }]
                that.setData({
                    qc_list: objcata.concat(data)
                })
            }
        })
    },
    jntsCate() {
        var that = this;
        share.wxRequest('crmapi/User/jntsCate', {}, function (res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var objcata=[{
                    "id": '',//分类ID
                    "title": "全部"//
                }]
                that.setData({
                    jncate: objcata.concat(data.lists)
                })
            }
        })
    },
    yesclcik(){
        var that = this;
        if (that.data.student_id !=''){
            wx.showLoading({
                title: '加载中~',
            })
            that.setData({
                zb_false: false,
            })
            var item = that.data.zb_item;
            var obj = {
                student_id: item.student_id,
                class_id: that.data.student_id,
            }
            share.wxRequest('crmapi/user/updStudent', obj, function (res) {
                wx.hideLoading();
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
                if (Number(res.data.errcode) == 0) {
                    setTimeout(function(){
                        that.lists();
                    },500)
                }
            })
        }else{
            wx.showToast({
                title: '请先选择班级',
                icon: 'none'
            })
        }
    },
    changeclass(e){
        var that = this;
        // console.log(e);
        var item = e.target.dataset.item || e.currentTarget.dataset.item;
        var index = e.target.dataset.index || e.currentTarget.dataset.index;
        var zb_arr = that.data.zb_arr;
        for (var i = 0; i < zb_arr.length;i++){
            if(Number(i)==Number(index)){
                zb_arr[i].choose_ch = 2;
            }else{
                zb_arr[i].choose_ch = 1;
            }
        }
        that.setData({
            zb_arr: zb_arr,
            student_id: that.data.zb_arr[index].id,
        })
    },
    zclass(e) {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var item = e.target.dataset.item || e.currentTarget.dataset.item;
        that.setData({
            zb_item:item,
            zb_arr: [],
            student_id:'',
        })
        that.chaClassList()
    },
    closeclcik(){
        var that = this;
        that.setData({
            zb_false: false,
        })
    },
    chaClassList() {
        var that = this;
        share.wxRequest('crmapi/user/chaClassList', { student_id: that.data.zb_item.student_id}, function (res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                if(Number(data.lists.length)>0){
                    for (var i = 0; i < data.lists.length;i++){
                        data.lists[i]['choose_ch'] = 1;
                    }
                    // console.log(data.lists);
                    that.setData({
                        zb_arr:data.lists,
                        zb_false:true,
                    })
                }else{
                    wx.showToast({
                        title: '暂无可转班数据',
                        icon: 'none'
                    })
                }
               
            }
            wx.hideLoading();
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        var page = Number(that.data.obj.page) + 1;
        that.setData({
            'obj.page':page
        })
        that.lists();
    },

})